package com.hd.manager.controller;


import com.hd.manager.shiro.token.PrivilegeCache;
import com.hd.manager.shiro.token.TokenManager;
import com.hd.appcommon.constant.PrivilegeConstant;
import com.hd.appcommon.entity.Privilege;
import com.hd.appcommon.entity.RolePrivilege;
import com.hd.appcommon.service.IPrivilegeService;
import com.hd.appcommon.service.IRolePrivilegeService;
import com.hd.hdxgddcommon.controller.AbstractController;
import com.hd.hdxgddcommon.entity.dto.ResultDto;
import com.hd.hdxgddcommon.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class PrivilegeController extends AbstractController {

    @Autowired
    private IPrivilegeService privilegeService;
    @Autowired
    private IRolePrivilegeService rolePrivilegeService;

    /**
     * 获取所有权限
     *
     * @return
     */
    @RequestMapping("/privilege/list")
    public ResultDto listByToken() {
        return success(TokenManager.getTokenPrivilegeTree());
    }

    /**
     * 当前 父级权限的子权限
     *
     * @param parentId 父级权限id
     * @return
     */
    @RequestMapping("/privilege/list/pid")
    public ResultDto listByParentId(@RequestParam String parentId) {
        List<Privilege> privileges = privilegeService.listByParentId(parentId);
        return success(privileges);
    }

    /**
     * 修改权限名称或者父地址
     *
     * @return
     */
    @RequestMapping("/privilege/edit")
    public ResultDto edit(Privilege privilege) {
        privilege.setModifyDate(new Date());
        boolean b = privilegeService.updateById(privilege);
        if (b) {
            PrivilegeCache.init(privilegeService.listAllAble());
        }
        return b ? success() : fail();
    }

    /**
     * 删除权限
     *
     * @param privilegeId 权限id
     * @param cascade     级联
     * @return
     */
    @RequestMapping("/privilege/remove")
    public ResultDto remove(String privilegeId, boolean cascade) {
        if (StringUtils.isBlank(privilegeId)) {
            return fail("请选择地址");
        }
        if (privilegeId.equals(PrivilegeConstant.ROOT_PRIVILEGE_ID))
            return fail("根权限不能删除!");
        Privilege byId = privilegeService.getById(privilegeId);
        if (byId == null) return fail("该权限不存在!");
        boolean flag;
        if (cascade) {
            flag = privilegeService.removeByIdCascade(privilegeId);
        } else {
            flag = privilegeService.removeById(privilegeId);
        }
        if (flag) {
            PrivilegeCache.init(privilegeService.listAllAble());
            // 删除用户对应权限
            rolePrivilegeService.removeByPrivilegeId(privilegeId);
            return success();
        }
        return fail();
    }

    /**
     * 批量删除权限
     *
     * @param privilegeIds 权限id集合
     * @return
     */
    @RequestMapping("/privilege/batch/remove")
    public ResultDto removeBatch(String[] privilegeIds) {
        if (StringUtils.isBlank(privilegeIds)) {
            return fail("请选择权限！");
        }
        boolean b = privilegeService.removeByIds(Arrays.asList(privilegeIds));
        if (b) {
            PrivilegeCache.init(privilegeService.listAllAble());
        }
        return b ? success("删除成功") : fail("删除失败");
    }

    /**
     * 添加权限
     *
     * @param privilege 权限信息
     * @return
     */
    @RequestMapping("/privilege/save")
    public ResultDto save(Privilege privilege) {
        Assert.hasText(privilege.getPrivilegeName(), "权限名不能为空!");
        Assert.hasText(privilege.getPrivilegeKey(), "权限关键字不能为空!");
        Assert.hasText(privilege.getParentId(), "权限上级id不能为空!");
        Privilege unique = privilegeService.getUnique(privilege);
        if (unique == null) {
            boolean save = privilegeService.save(privilege);
            if (save) {
                //
                PrivilegeCache.init(privilegeService.listAllAble());
                //新增用户权限
                String parentId = privilege.getParentId();
                List<RolePrivilege> rolePrivileges = rolePrivilegeService.listByPrivilegeId(parentId);
                String privilegeId = privilege.getPrivilegeId();
                List<RolePrivilege> collect = rolePrivileges.stream().map(rp -> {
                    RolePrivilege rolePrivilege = new RolePrivilege();
                    rolePrivilege.setRoleId(rp.getRoleId()).setPrivilegeId(privilegeId);
                    return rolePrivilege;
                }).collect(Collectors.toList());
                rolePrivilegeService.saveBatch(collect);
            }
            return save ? success() : fail("保存失败");
        } else
            return fail("该权限已存在!");

    }


}
